package cn.user.controller;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("user")
public class UserController {
   
    
    

    @GetMapping("test")
    public Object getByName(){
        return  getLoginAppUser();
    }
    
    
    public static synchronized Object getLoginAppUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
         if (authentication instanceof OAuth2Authentication) {
             OAuth2Authentication oAuth2Auth = (OAuth2Authentication) authentication;
             authentication = oAuth2Auth.getUserAuthentication();

             if (authentication instanceof UsernamePasswordAuthenticationToken) {
                 UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) authentication;
                 Object principal = authentication.getPrincipal();
                 return principal;
             }
         }
         return null;
    }
   
}
